/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.filters;

import com.cv4j.core.datamodel.ColorProcessor;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.image.util.Tools;

import java.util.Optional;

/**
 * 浮雕过滤器
 *
 * @author dev
 * @date 2021/07/22
 */
public class EmbossFilter extends BaseFilter {
    private int COLORCONSTANTS;
    private boolean out;

    /**
     * 浮雕过滤器
     */
    public EmbossFilter() {
        this.COLORCONSTANTS = 100;
    }

    /**
     * 浮雕过滤器
     *
     * @param out 出
     */
    public EmbossFilter(boolean out) {
        this.out = out;
        this.COLORCONSTANTS = 100;
    }

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link Optional<ImageProcessor>}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        int offset = 0;
        int r1 = 0;
        int g1 = 0;
        int b1 = 0;
        int r2 = 0;
        int g2 = 0;
        int b2 = 0;
        int red = 0;
        int greed = 0;
        int blue = 0;

        byte[][] output = new byte[3][RED.length];
        for (int y = 1; y < height - 1; y++) {
            offset = y * width;
            for (int x = 1; x < width - 1; x++) {
                r1 = RED[offset] & 0xff;
                g1 = GREED[offset] & 0xff;
                b1 = BLUE[offset] & 0xff;

                r2 = RED[offset + width] & 0xff;
                g2 = GREED[offset + width] & 0xff;
                b2 = BLUE[offset + width] & 0xff;

                if (out) {
                    red = r1 - r2;
                    greed = g1 - g2;
                    blue = b1 - b2;
                } else {
                    red = r2 - r1;
                    greed = g2 - g1;
                    blue = b2 - b1;
                }
                red = Tools.clamp(red + COLORCONSTANTS);
                greed = Tools.clamp(greed + COLORCONSTANTS);
                blue = Tools.clamp(blue + COLORCONSTANTS);

                output[0][offset] = (byte) red;
                output[1][offset] = (byte) greed;
                output[2][offset] = (byte) blue;
                offset++;
            }
        }
        ((ColorProcessor) src).putRGB(output[0], output[1], output[2]);
        output = null;
        return Optional.ofNullable(src);
    }

    /**
     * 出发
     * setOUT
     *
     * @param out out
     */
    public void setOUT(boolean out) {
        this.out = out;
    }
}
